package com.xmmj.baselibrary.utils.decoration

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import androidx.recyclerview.widget.RecyclerView
import kotlin.jvm.JvmOverloads
import androidx.recyclerview.widget.LinearLayoutManager

/**
 * 垂直线性分割线
 */
class VerticalSpaceItemDecoration @JvmOverloads constructor(
    private val mSpace: Int,
    private val mFirstTopSpace: Int = 0,
    private val mLastBottomSpace: Int = 0
) : ItemDecoration() {
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.getChildAdapterPosition(view) > 0) {
            outRect.top = mSpace
        } else if (mFirstTopSpace > 0 && parent.getChildAdapterPosition(view) == 0) {
            outRect.top = mFirstTopSpace
        }
        val layoutManager = parent.layoutManager as LinearLayoutManager?
        if (mLastBottomSpace > 0 && parent.getChildAdapterPosition(view) == layoutManager!!.itemCount - 1) {
            outRect.bottom = mLastBottomSpace
        }
    }
}